package dfs;

/**
 * 二、 二叉树的深搜
 * 2. 求根节点到叶子结点数字之和
 * 2024-11-5
 */
public class demo7 {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    public int dfs(TreeNode root, int preSum) {
        preSum = preSum * 10 + root.val;
        if(root.left == null && root.right == null) {
            return preSum;
        }

        int ret = 0;
        if(root.left != null) ret += dfs(root.left, preSum);
        if(root.right != null) ret += dfs(root.right, preSum);

        return ret;
    }
}
